<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/20
 * Time: 16:54
 */

namespace app\modules\setting\controllers\seller;


use app\common\controllers\AdminOptionsController;
use app\common\helper\CommonHelper;
use app\models\EtcOfficeVideoUpload;
use app\modules\setting\configs\seller\SellingConfig;
use app\modules\setting\models\seller\EtcOfficeProductSelectedSearch;
use sookie\auth\models\Adminuser;
use Yii;
use yii\db\Expression;
use yii\helpers\ArrayHelper;

class SellingController extends AdminOptionsController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new EtcOfficeProductSelectedSearch();
            $dataProvider = $searchModel->searchSelling(Yii::$app->request->queryParams);
            $data = $dataProvider->getModels();
            $info = Adminuser::getUserInfo();
            if($data){
                $data = array_map(function($item) use ($info){
                    if(is_object($item)){
                        $item = $item->toArray();
                    }
                    $count = EtcOfficeVideoUpload::find()
                        ->select([
                            new Expression("count(*) video_num"),
                            new Expression("count(distinct tiktok_id) tiktok_account_num"),
                        ])
                        ->andWhere(['selected_id'=>$item['id']])->asArray()->one();
                    $item['video_num'] = floatval(ArrayHelper::getValue($count,'video_num'));
                    $item['tiktok_account_num'] = floatval(ArrayHelper::getValue($count,'tiktok_account_num'));
                    $item['selected_id'] = $item['id'];
                    $item['product_id'] = CommonHelper::getProductNameById($item['product_id']);
                    return $item;
                },$data);
            }
            return ArrayHelper::merge(["data" => $data, "pagination" => $dataProvider->getPagination()],SellingConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }
}